<?php
namespace Home\Controller;
use Think\Controller;
// 本类中需要控制权限，所以要集成权限控制类
class PersonController extends AuthController {


    public function index(){


       $this->display();   //引入模板，就可以在模板输出相应的信息
    }

  
    public function set_face(){
        $this->display();
    }


    //reset方法用于接收和修改信息
    //这里接到参数是post方式

    //修改
    public function reset(){
    	//修改我的资料
    	//接收数据
    	$email = $_POST['email'];
    	$nickname = $_POST['nickname'];
    	$sign = $_POST['sign'];
    	$city = $_POST['city'];
    	$sex = $_POST['sex'];
    	//修改当前数据
    	$id = $_SESSION['id'];

    	$sql = "update user set email='$email',nickname='$nickname',sign='$sign',city='$city',sex='$sex' where id='".$id."'";

    	     
        $res = M()->execute($sql);
       

        if($res){
        	$arr = array(
                'error'=>0,
                'info'=>'修改成功'
        	);

        	echo json_encode($arr);
           
        }else{
        	$arr = array(
                'error'=>1,
                'info'=>'修改失败'
        	);

        	echo json_encode($arr);
        }


        header('Location:'.U('home/person/index'));

        
    } 



    public function repass(){
        if(!empty($_POST['password'])){
 
            //接收数据
            $password = $_POST['password'];
            // $password = md5($password); 
            //修改
            $id = $_SESSION['id'];
            $sql = "UPDATE user SET 
                        password='$password' 
                    WHERE id=".$id;
            
            $psw = M()->execute($sql);
            if($psw){
                $arr = array(
                    'error'=>0,
                    'info'=>'修改成功'
                );
                echo json_encode($arr);exit;
            }else{
                $arr = array(
                    'error'=>1,
                    'info'=>'修改失败'
                );

                echo json_encode($arr);exit;
            }

        }
        else{

            $this->display();
        }
    }


    public function upload()
    {

    	$upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
	    $upload->savePath  =     ''; // 设置附件上传（子）目录
	    // 上传文件 
	    $info   =   $upload->upload();

	    if(!$info) {// 上传错误提示错误信息
	        echo $upload->getError();
	    }else{// 上传成功
	    	$info = current($info);
	    	$path = __ROOT__."/Uploads/".$info['savepath'].$info['savename'];

            $sql = "UPDATE user SET face = '$path' WHERE id = '{$_SESSION['id']}'  ";

            M()->execute($sql);

            echo 'upload success';

            $this->redirect('set_face');
	    }


    }


    //用户信息
    public function userinfo(){

       
        //个人信息
        $uid = $_GET['id'];
        //print_r($uid);
        $ss = "SELECT face,city,sign,nickname,create_time,kiss FROM user WHERE id='$uid'";

        $per = M()->query($ss);
        $this->assign('per',$per[0]);

        //最近提问
        $tiwens = "SELECT id,title,answer_num,view_num,create_time,content FROM question WHERE uid='$uid'";
        $tiwen = M()->query($tiwens);
        //var_dump($tiwen);
        $this->assign('tiwen',$tiwen);


        //最近回答
        $qid = $_GET['qid'];
        $huidas = "SELECT t2.title,t1.create_time,t1.content,t1.qid FROM answer t1 inner join question t2 on t1.qid=t2.id WHERE t1.uid='$uid'";
        $huida= M()->query($huidas);
        //var_dump($huida);
        $this->assign('huida',$huida);

        $this->display();
    }




    //用户中心
    public function user_center(){
        //我发的帖子
        $issues = "SELECT id,title,create_time,view_num,answer_num FROM question WHERE uid = '".$_SESSION['id']."' ";
        $issue = M()->query($issues);
        //print_r($issue);
        $this->assign('issue',$issue);

        $this->display();
    }

    //我的消息
    public function msg(){
        $this->display();
    }


    //产品授权
    public function pro_auth(){
        $this->display();
    }


    //发送邮件
    public function sendemail(){
        $email = '123@qq.com';      //接收人
        $subject = 'subject ';      //发送主题
        $message = 'Hello World! This is a simple email message';  //发送内容

        //mail()函数：用于脚本发送邮件
        mail($email,$subject,$message);
        $this->display();
    }


    //激活邮箱
    public function active(){
        //接收表单提交过来的信息
        $post = $_POST;
        var_dump($post);

        $this->display();

    }
        

}